package cn.chenxing.module.main.controller;

import cn.chenxing.domain.util.FileChooserUtil;
import cn.chenxing.domain.threadlocal.ThreadLocalInfo;
import cn.chenxing.domain.util.SpringContextUtil;
import cn.chenxing.constant.VenusConstant;
import cn.chenxing.module.main.service.MainService;
import com.jfoenix.controls.JFXButton;
import de.felixroske.jfxsupport.AbstractFxmlView;
import de.felixroske.jfxsupport.FXMLController;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;

import java.io.File;
import java.net.URL;
import java.util.ResourceBundle;

/**
 * @Description 主界面Controller层
 * @Author maogen.ymg
 * @Date 2020/3/11 12:30
 */
@Lazy
@FXMLController
@Slf4j
public class MainController implements Initializable {

    @FXML
    private JFXButton headImage;

    @FXML
    private Label welcome;

    @FXML
    private JFXButton fileMenu;

    @FXML
    private JFXButton queryMenu;

    @FXML
    private JFXButton analysisMenu;

    @FXML
    private JFXButton visualMenu;

    @FXML
    private JFXButton aboutMenu;

    @FXML
    private AnchorPane anchorPane;

    @Autowired
    MainService mainService;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
         init();
    }

    private void init() {

        // 设置菜单按钮图标
        mainService.setMenuIcon("/images/main/menu/file.png", fileMenu);
        mainService.setMenuIcon("/images/main/menu/query.png", queryMenu);
        mainService.setMenuIcon("/images/main/menu/analysis.png", analysisMenu);
        mainService.setMenuIcon("/images/main/menu/visual.png", visualMenu);
        mainService.setMenuIcon("/images/main/menu/about.png", aboutMenu);

        // 设置欢迎语
        welcome.setText(String.format("%s, %s!", ThreadLocalInfo.get().getUserName(), mainService.getWelcomeSpeech()));
        // 设置头像
        mainService.setHeadImage(headImage);

        // 初始为文件页面
        switchFileMenu();
    }

    @FXML
    private void changeHeadImage() {
        File folder = FileChooserUtil.chooseImageFile("请选择本地头像");
        boolean isUpload = mainService.uploadHeadImage(folder.getPath(), 80, 80);
        if(isUpload){
            mainService.setHeadImage(headImage);
        }
    }

    @FXML
    private void switchFileMenu() {
        try{
            removeView();
            addView(VenusConstant.FILE_VIEW_CLASS);
            addView(VenusConstant.FILE_LOCAL_VIEW_CLASS);
            addView(VenusConstant.FILE_ONLINE_VIEW_CLASS);
        }catch (ClassNotFoundException e){
            log.error("切换文件菜单出错: {}", e.getMessage(), e);
        }

    }

    @FXML
    private void switchQueryMenu() {
        try{
            removeView();
            addView(VenusConstant.QUERY_VIEW_CLASS);
        }catch (ClassNotFoundException e){
            log.error("切换查询菜单出错: {}", e.getMessage(), e);
        }
    }

    @FXML
    private void switchAnalysis() {
        try{
            removeView();
            addView(VenusConstant.ANALYSIS_VIEW_CLASS);
        }catch (ClassNotFoundException e){
            log.error("切换分析菜单出错: {}", e.getMessage(), e);
        }
    }

    @FXML
    private void switchVisualMenu() {
        try{
            removeView();
            addView(VenusConstant.VISUAL_VIEW_CLASS);
        }catch (ClassNotFoundException e){
            log.error("切换可视化菜单出错: {}", e.getMessage(), e);
        }
    }

    /**
     * 添加View到anchorPane
     * @param viewClassName view类名
     * @throws ClassNotFoundException 异常
     */
    @SuppressWarnings("unchecked")
    private void addView(String viewClassName) throws ClassNotFoundException {
        Class<AbstractFxmlView> viewClass = (Class<AbstractFxmlView>) ClassLoader.getSystemClassLoader().loadClass(viewClassName);
        AbstractFxmlView fxmlView = SpringContextUtil.getBean(viewClass);
        anchorPane.getChildren().addAll(fxmlView.getView());
    }

    /**
     * 移除anchorPane的所有内容
     */
    private void removeView() {
        anchorPane.getChildren().clear();
    }


}
